package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.CosTemplate;
import com.tanhua.commons.utils.ImagesStreamUtils;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MovementService {

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private CosTemplate cosTemplate;

    @DubboReference
    private UserInfoApi userInfoApi;

    public void publishMovement(Movement movement, MultipartFile[] imageContent) throws IOException {
        if (StringUtils.isEmpty(movement.getTextContent())) {
            throw new BusinessException(ErrorResult.contentError());
        }
        List<String> medias = new ArrayList<>();
        for (MultipartFile multipartFile : imageContent) {
            File file = ImagesStreamUtils.convertMultipartFiletoFile(multipartFile);
            String upload = cosTemplate.upload(file, file.getName());
            medias.add(upload);
        }
        Long userId = UserHolder.getUserId();
        movement.setUserId(userId);
        movement.setMedias(medias);
        movementApi.publish(movement);

    }


    public PageResult findById(Long userId, Integer page, Integer pagesize) {
        PageResult pr = movementApi.findById(userId, page, pagesize);
        List<Movement> items = (List<Movement>) pr.getItems();
        if (items == null) {
            return pr;
        }
        UserInfo userInfo = userInfoApi.findById(userId);
        ArrayList<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : items) {
            MovementsVo vo = MovementsVo.init(userInfo, movement);
            vos.add(vo);
        }
        pr.setItems(vos);
        return pr;
    }

    // 查询好友的动态test
    public PageResult findFridenMovements(Integer page, Integer pagesize) {
        Long userId = UserHolder.getUserId();
        List<Movement> list = movementApi.findFriendMovements(page, pagesize, userId);
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : list) {
            UserInfo userInfo = map.get(movement.getUserId());
            if (userInfo != null) {
                MovementsVo vo = MovementsVo.init(userInfo, movement);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0L, vos);
    }
}
